/*********************************************************************
	Rhapsody	: 7.5.2
	Login		: michalwski
	Component	: Lib
	Configuration 	: DefaultConfig
	Model Element	: Engine
//!	Generated Date	: Thu, 6, Jan 2011 
	File Path	: Lib/DefaultConfig/RobotPkg/Engine.java
*********************************************************************/

package RobotPkg;

//## auto_generated
import java.util.*;
//## auto_generated
import com.ibm.rational.rhapsody.oxf.*;
//## auto_generated
import com.ibm.rational.rhapsody.oxf.states.*;

//----------------------------------------------------------------------------
// RobotPkg/Engine.java                                                                  
//----------------------------------------------------------------------------

//## package RobotPkg 


//## class Engine 
public class Engine implements RiJStateConcept, IEngine {
    
    public Reactive reactive;		//## ignore 
    
    protected int direction;		//## attribute direction 
    
    protected int id;		//## attribute id 
    
    protected LinkedList<IEngineListener> listeners;		//## attribute listeners 
    
    protected int speed;		//## attribute speed 
    
    protected RobotController itsRobotController;		//## link itsRobotController 
    
    //#[ ignore 
    public static final int RiJNonState=0;
    public static final int on=1;
    public static final int rotation=2;
    public static final int forward=3;
    public static final int backward=4;
    public static final int ready=5;
    public static final int off=6;
    //#]
    protected int rootState_subState;		//## ignore 
    
    protected int rootState_active;		//## ignore 
    
    protected int on_subState;		//## ignore 
    
    protected int rotation_subState;		//## ignore 
    
    
    //## statechart_method 
    public RiJThread getThread() {
        return reactive.getThread();
    }
    
    //## statechart_method 
    public void schedTimeout(long delay, long tmID, RiJStateReactive reactive) {
        getThread().schedTimeout(delay, tmID, reactive);
    }
    
    //## statechart_method 
    public void unschedTimeout(long tmID, RiJStateReactive reactive) {
        getThread().unschedTimeout(tmID, reactive);
    }
    
    //## statechart_method 
    public boolean isIn(int state) {
        return reactive.isIn(state);
    }
    
    //## statechart_method 
    public boolean isCompleted(int state) {
        return reactive.isCompleted(state);
    }
    
    //## statechart_method 
    public RiJEventConsumer getEventConsumer() {
        return (RiJEventConsumer)reactive;
    }
    
    //## statechart_method 
    public void gen(RiJEvent event) {
        reactive._gen(event);
    }
    
    //## statechart_method 
    public void queueEvent(RiJEvent event) {
        reactive.queueEvent(event);
    }
    
    //## statechart_method 
    public int takeEvent(RiJEvent event) {
        return reactive.takeEvent(event);
    }
    
    // Constructors
    
    //## auto_generated 
    public  Engine(RiJThread p_thread) {
        reactive = new Reactive(p_thread);
        {
            listeners = new LinkedList<IEngineListener>();
        }
    }
    
    //## operation addListener(IEngineListener) 
    public void addListener(final IEngineListener listener) {
        //#[ operation addListener(IEngineListener) 
        listeners.add(listener);
        //#]
    }
    
    //## operation getDirection() 
    public int getDirection() {
        //#[ operation getDirection() 
        return direction;
        //#]
    }
    
    //## operation getSpeed() 
    public int getSpeed() {
        //#[ operation getSpeed() 
        return speed;
        //#]
    }
    
    //## operation setDirection(int) 
    public void setDirection(int direction) {
        //#[ operation setDirection(int) 
        this.direction = direction;
        //#]
    }
    
    //## operation setSpeed(int) 
    public void setSpeed(int speed) {
        //#[ operation setSpeed(int) 
        this.speed = speed;
        //#]
    }
    
    //## auto_generated 
    public int getId() {
        return id;
    }
    
    //## auto_generated 
    public void setId(int p_id) {
        id = p_id;
    }
    
    //## auto_generated 
    public ListIterator<IEngineListener> getListeners() {
        ListIterator<IEngineListener> iter = listeners.listIterator();
        return iter;
    }
    
    //## auto_generated 
    public void setListeners(IEngineListener p_listeners) {
        listeners.add(p_listeners);
    }
    
    //## auto_generated 
    public RobotController getItsRobotController() {
        return itsRobotController;
    }
    
    //## auto_generated 
    public void __setItsRobotController(RobotController p_RobotController) {
        itsRobotController = p_RobotController;
    }
    
    //## auto_generated 
    public void _setItsRobotController(RobotController p_RobotController) {
        if(itsRobotController != null)
            {
                itsRobotController._removeItsEngine(this);
            }
        __setItsRobotController(p_RobotController);
    }
    
    //## auto_generated 
    public void setItsRobotController(RobotController p_RobotController) {
        if(p_RobotController != null)
            {
                p_RobotController._addItsEngine(this);
            }
        _setItsRobotController(p_RobotController);
    }
    
    //## auto_generated 
    public void _clearItsRobotController() {
        itsRobotController = null;
    }
    
    //## auto_generated 
    public boolean startBehavior() {
        boolean done = false;
        done = reactive.startBehavior();
        return done;
    }
    
    //## ignore 
    public class Reactive extends RiJStateReactive {
        
        // Default constructor 
        public Reactive() {
            this(RiJMainThread.instance());
        }
        
        
        // Constructors
        
        public  Reactive(RiJThread p_thread) {
            super(p_thread);
            initStatechart();
        }
        
        //## statechart_method 
        public boolean isIn(int state) {
            if(rotation_subState == state)
                {
                    return true;
                }
            if(on_subState == state)
                {
                    return true;
                }
            if(rootState_subState == state)
                {
                    return true;
                }
            return false;
        }
        
        //## statechart_method 
        public boolean isCompleted(int state) {
            return true;
        }
        
        //## statechart_method 
        public void rootState_entDef() {
            {
                rootState_enter();
                rootStateEntDef();
            }
        }
        
        //## statechart_method 
        public int rootState_dispatchEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            switch (rootState_active) {
                case ready:
                {
                    res = ready_takeEvent(id);
                }
                break;
                case forward:
                {
                    res = forward_takeEvent(id);
                }
                break;
                case backward:
                {
                    res = backward_takeEvent(id);
                }
                break;
                case off:
                {
                    res = off_takeEvent(id);
                }
                break;
                default:
                    break;
            }
            return res;
        }
        
        //## auto_generated 
        protected void initStatechart() {
            rootState_subState = RiJNonState;
            rootState_active = RiJNonState;
            on_subState = RiJNonState;
            rotation_subState = RiJNonState;
        }
        
        //## statechart_method 
        public void offEnter() {
        }
        
        //## statechart_method 
        public void backward_entDef() {
            backward_enter();
        }
        
        //## statechart_method 
        public void forward_enter() {
            rotation_subState = forward;
            rootState_active = forward;
            forwardEnter();
        }
        
        //## statechart_method 
        public void forward_entDef() {
            forward_enter();
        }
        
        //## statechart_method 
        public void ready_enter() {
            on_subState = ready;
            rootState_active = ready;
            readyEnter();
        }
        
        //## statechart_method 
        public void backward_exit() {
            backwardExit();
        }
        
        //## statechart_method 
        public int rotationTakeevChangeDirection() {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            rotation_exit();
            ready_entDef();
            res = RiJStateReactive.TAKE_EVENT_COMPLETE;
            return res;
        }
        
        //## statechart_method 
        public void off_exit() {
            offExit();
        }
        
        //## statechart_method 
        public void offExit() {
        }
        
        //## statechart_method 
        public void onEnter() {
        }
        
        //## statechart_method 
        public int rootState_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            return res;
        }
        
        //## statechart_method 
        public int readyTakeevMove() {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            //## transition 4 
            if(getDirection() == 0)
                {
                    ready_exit();
                    rotation_enter();
                    backward_entDef();
                    res = RiJStateReactive.TAKE_EVENT_COMPLETE;
                }
            else
                {
                    ready_exit();
                    rotation_enter();
                    forward_entDef();
                    res = RiJStateReactive.TAKE_EVENT_COMPLETE;
                }
            return res;
        }
        
        //## statechart_method 
        public void on_exit() {
            switch (on_subState) {
                case ready:
                {
                    ready_exit();
                }
                break;
                case rotation:
                {
                    rotation_exit();
                }
                break;
                default:
                    break;
            }
            on_subState = RiJNonState;
            onExit();
        }
        
        //## statechart_method 
        public void on_entDef() {
            on_enter();
            
            ready_entDef();
        }
        
        //## statechart_method 
        public void ready_entDef() {
            ready_enter();
        }
        
        //## statechart_method 
        public int on_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(evOnOff.evOnOff_RobotPkg_id))
                {
                    res = onTakeevOnOff();
                }
            
            return res;
        }
        
        //## statechart_method 
        public void rootState_enter() {
            rootStateEnter();
        }
        
        //## statechart_method 
        public void rootStateEnter() {
        }
        
        //## statechart_method 
        public int offTakeevOnOff() {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            off_exit();
            on_entDef();
            res = RiJStateReactive.TAKE_EVENT_COMPLETE;
            return res;
        }
        
        //## statechart_method 
        public void off_enter() {
            rootState_subState = off;
            rootState_active = off;
            offEnter();
        }
        
        //## statechart_method 
        public void rotationEnter() {
        }
        
        //## statechart_method 
        public void onExit() {
        }
        
        //## statechart_method 
        public void forwardExit() {
        }
        
        //## statechart_method 
        public void backward_enter() {
            rotation_subState = backward;
            rootState_active = backward;
            backwardEnter();
        }
        
        //## statechart_method 
        public void forwardEnter() {
            //#[ state ROOT.on.rotation.forward.(Entry) 
            if(getSpeed()>0){
              System.out.println("Silnik = "+id+" - Polaryzacja do przodu");
              for(IEngineListener l : listeners){
                l.go();
              }
            }
            else{
              System.out.println("Silnik = "+id+" - Silnik stop");
              for(IEngineListener l : listeners){
                l.stop();
              }
            }
            //#]
        }
        
        //## statechart_method 
        public void on_enter() {
            rootState_subState = on;
            onEnter();
        }
        
        //## statechart_method 
        public void rootState_exit() {
            switch (rootState_subState) {
                case on:
                {
                    on_exit();
                }
                break;
                case off:
                {
                    off_exit();
                }
                break;
                default:
                    break;
            }
            rootState_subState = RiJNonState;
            rootStateExit();
        }
        
        //## statechart_method 
        public void rootStateEntDef() {
            off_entDef();
        }
        
        //## statechart_method 
        public int off_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(evOnOff.evOnOff_RobotPkg_id))
                {
                    res = offTakeevOnOff();
                }
            
            return res;
        }
        
        //## statechart_method 
        public void readyEnter() {
        }
        
        //## statechart_method 
        public void rotation_exit() {
            switch (rotation_subState) {
                case forward:
                {
                    forward_exit();
                }
                break;
                case backward:
                {
                    backward_exit();
                }
                break;
                default:
                    break;
            }
            rotation_subState = RiJNonState;
            rotationExit();
        }
        
        //## statechart_method 
        public void readyExit() {
            //#[ state ROOT.on.ready.(Exit) 
            System.out.println("Silnik = "+id+" Ruch");
            //#]
        }
        
        //## statechart_method 
        public int rotation_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(evChangeDirection.evChangeDirection_RobotPkg_id))
                {
                    res = rotationTakeevChangeDirection();
                }
            
            if(res == RiJStateReactive.TAKE_EVENT_NOT_CONSUMED)
                {
                    res = on_takeEvent(id);
                }
            return res;
        }
        
        //## statechart_method 
        public int onTakeevOnOff() {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            on_exit();
            off_entDef();
            res = RiJStateReactive.TAKE_EVENT_COMPLETE;
            return res;
        }
        
        //## statechart_method 
        public void backwardExit() {
        }
        
        //## statechart_method 
        public void forward_exit() {
            forwardExit();
        }
        
        //## statechart_method 
        public void rotationExit() {
            //#[ state ROOT.on.rotation.(Exit) 
            System.out.println("Silnik = "+id+" - Zmiana kierunku");
            //#]
        }
        
        //## statechart_method 
        public void rootStateExit() {
        }
        
        //## statechart_method 
        public int ready_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            if(event.isTypeOf(evMove.evMove_RobotPkg_id))
                {
                    res = readyTakeevMove();
                }
            
            if(res == RiJStateReactive.TAKE_EVENT_NOT_CONSUMED)
                {
                    res = on_takeEvent(id);
                }
            return res;
        }
        
        //## statechart_method 
        public void ready_exit() {
            readyExit();
        }
        
        //## statechart_method 
        public void backwardEnter() {
            //#[ state ROOT.on.rotation.backward.(Entry) 
            if(getSpeed()>0){
              System.out.println("Silnik = "+id+" - Polaryzacja do tylu");
              for(IEngineListener l : listeners){
                l.back();
              }
            }
            else{
              System.out.println("Silnik = "+id+" - Silnik stop");
              for(IEngineListener l : listeners){
                l.stop();
              }
            }
            //#]
        }
        
        //## statechart_method 
        public int forward_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            res = rotation_takeEvent(id);
            return res;
        }
        
        //## statechart_method 
        public void rotation_entDef() {
            rotation_enter();
            
            forward_entDef();
        }
        
        //## statechart_method 
        public void off_entDef() {
            off_enter();
        }
        
        //## statechart_method 
        public int backward_takeEvent(short id) {
            int res = RiJStateReactive.TAKE_EVENT_NOT_CONSUMED;
            res = rotation_takeEvent(id);
            return res;
        }
        
        //## statechart_method 
        public void rotation_enter() {
            on_subState = rotation;
            rotationEnter();
        }
        
    }
}
/*********************************************************************
	File Path	: Lib/DefaultConfig/RobotPkg/Engine.java
*********************************************************************/

